package com.lhdx.practical.service.impl;

import com.lhdx.practical.bean.StudentGroup;
import com.lhdx.practical.dao.StudentGroupDao;
import com.lhdx.practical.service.StudentGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service("StudentGroupService")
public class StudentGroupServiceImpl implements StudentGroupService {
    @Autowired
    private StudentGroupDao studentGroupDao;

    @Override
    public void save(StudentGroup studentGroup) {
        studentGroupDao.save(studentGroup);
    }

    @Override
    public void deleteByID(long id) {
        studentGroupDao.deleteById(id);
    }

    @Override
    public StudentGroup findById(long id) {
        return studentGroupDao.findById(id);
    }

    @Override
    public StudentGroup findByFounderid(long founderid) {
        return studentGroupDao.findByFounderid(founderid);
    }

    @Override
    public Page<StudentGroup> getResultList(long departid,int state,long gradeid, int page, int pagesize) {
        Sort sort = new Sort(Sort.Direction.DESC, "createtime");
        Pageable pageable = PageRequest.of(page == 0 ? 0 : page -1, pagesize, sort);
        Page<StudentGroup> data = studentGroupDao.findAll(new Specification<StudentGroup>(){
            @Override
            public Predicate toPredicate(Root<StudentGroup> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                List<Predicate> list = new ArrayList<Predicate>();
                if(departid > 0){
                    list.add(cb.equal(root.get("departid"),departid));
                }
                if(state != -1){
                    list.add(cb.equal(root.get("state"),state));
                }else if(state == -1){
                    list.add(cb.or(cb.equal(root.get("state"),1),cb.equal(root.get("state"),2)));
                }
                if(gradeid != 0){
                    list.add(cb.equal(root.get("gradeid"),gradeid));
                }
                Predicate[] p = new Predicate[list.size()];
                return cb.and(list.toArray(p));
            }
        },pageable);
        return data;
    }

    @Override
    public List<StudentGroup> findAllByDepartid(long departid,int state) {
        return studentGroupDao.findAllByDepartidAndStateOrderByCreatetimeDesc(departid,state);
    }

    @Override
    public void updateGroupCount(long id, int groupcount) {
        studentGroupDao.updateGroupCount(id,groupcount);
    }

    @Override
    public void updateState(long id, int state) {
        studentGroupDao.updateState(id,state);
    }
}
